Option Explicit On Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form Dim sq1 As square ' square to work with #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TxtSide As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtArea As System.Windows.Forms.TextBox Friend WithEvents TxtPerim As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents BtnQuit As System.Windows.Forms.Button Friend WithEvents Sq As System.Windows.Forms.Button Friend WithEvents TxtX As System.Windows.Forms.TextBox Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents TxtY As System.Windows.Forms.TextBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents TxtName As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents CmbBack As System.Windows.Forms.ComboBox Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents CmbFore As System.Windows.Forms.ComboBox Friend WithEvents BtnDown As System.Windows.Forms.Button Friend WithEvents BtnUp As System.Windows.Forms.Button Friend WithEvents BtnLeft As System.Windows.Forms.Button Friend WithEvents BtnRight As System.Windows.Forms.Button Friend WithEvents TxtTimes As System.Windows.Forms.TextBox Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.Button1 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.TxtSide = New System.Windows.Forms.TextBox Me.TxtArea = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.TxtPerim = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.BtnQuit = New System.Windows.Forms.Button Me.Sq = New System.Windows.Forms.Button Me.TxtX = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.TxtY = New System.Windows.Forms.TextBox Me.Label5 = New System.Windows.Forms.Label Me.TxtName = New System.Windows.Forms.TextBox Me.Label6 = New System.Windows.Forms.Label Me.CmbBack = New System.Windows.Forms.ComboBox Me.Label7 = New System.Windows.Forms.Label Me.Label8 = New System.Windows.Forms.Label Me.CmbFore = New System.Windows.Forms.ComboBox Me.BtnDown = New System.Windows.Forms.Button Me.BtnUp = New System.Windows.Forms.Button Me.BtnLeft = New System.Windows.Forms.Button Me.BtnRight = New System.Windows.Forms.Button Me.TxtTimes = New System.Windows.Forms.TextBox Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(184, 32) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(32, 24) Me.Button1.TabIndex = 0 Me.Button1.Text = "Go" ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(16, 32) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(88, 24) Me.Label1.TabIndex = 1 Me.Label1.Text = "Size:" ' 'TxtSide ' Me.TxtSide.Location = New System.Drawing.Point(96, 32) Me.TxtSide.Name = "TxtSide" Me.TxtSide.Size = New System.Drawing.Size(80, 20) Me.TxtSide.TabIndex = 2 Me.TxtSide.Text = "" ' 'TxtArea ' Me.TxtArea.Location = New System.Drawing.Point(256, 280) Me.TxtArea.Name = "TxtArea" Me.TxtArea.ReadOnly = True Me.TxtArea.Size = New System.Drawing.Size(80, 20) Me.TxtArea.TabIndex = 4 Me.TxtArea.Text = "" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(176, 280) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(88, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "Area" ' 'TxtPerim ' Me.TxtPerim.Location = New System.Drawing.Point(256, 320) Me.TxtPerim.Name = "TxtPerim" Me.TxtPerim.ReadOnly = True Me.TxtPerim.Size = New System.Drawing.Size(80, 20) Me.TxtPerim.TabIndex = 6 Me.TxtPerim.Text = "" ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.Location = New System.Drawing.Point(176, 320) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(88, 24) Me.Label3.TabIndex = 5 Me.Label3.Text = "Perimeter" ' 'BtnQuit ' Me.BtnQuit.Location = New System.Drawing.Point(232, 32) Me.BtnQuit.Name = "BtnQuit" Me.BtnQuit.Size = New System.Drawing.Size(48, 24) Me.BtnQuit.TabIndex = 7 Me.BtnQuit.Text = "Quit" ' 'Sq ' Me.Sq.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(128, Byte)) Me.Sq.ForeColor = System.Drawing.Color.Red Me.Sq.Location = New System.Drawing.Point(224, 112) Me.Sq.Name = "Sq" Me.Sq.Size = New System.Drawing.Size(40, 24) Me.Sq.TabIndex = 8 Me.Sq.Text = "-" ' 'TxtX ' Me.TxtX.Location = New System.Drawing.Point(96, 64) Me.TxtX.Name = "TxtX" Me.TxtX.Size = New System.Drawing.Size(80, 20) Me.TxtX.TabIndex = 10 Me.TxtX.Text = "" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.Location = New System.Drawing.Point(16, 64) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(88, 24) Me.Label4.TabIndex = 9 Me.Label4.Text = "X:" ' 'TxtY ' Me.TxtY.Location = New System.Drawing.Point(96, 96) Me.TxtY.Name = "TxtY" Me.TxtY.Size = New System.Drawing.Size(80, 20) Me.TxtY.TabIndex = 12 Me.TxtY.Text = "" ' 'Label5 ' Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label5.Location = New System.Drawing.Point(16, 96) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(88, 24) Me.Label5.TabIndex = 11 Me.Label5.Text = "Y:" ' 'TxtName ' Me.TxtName.Location = New System.Drawing.Point(96, 128) Me.TxtName.Name = "TxtName" Me.TxtName.Size = New System.Drawing.Size(80, 20) Me.TxtName.TabIndex = 14 Me.TxtName.Text = "" ' 'Label6 ' Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label6.Location = New System.Drawing.Point(16, 128) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(88, 24) Me.Label6.TabIndex = 13 Me.Label6.Text = "Text:" ' 'CmbBack ' Me.CmbBack.Items.AddRange(New Object() {"Gray", "Green", "Blue", "Yellow", "Orange"}) Me.CmbBack.Location = New System.Drawing.Point(96, 160) Me.CmbBack.Name = "CmbBack" Me.CmbBack.Size = New System.Drawing.Size(96, 21) Me.CmbBack.TabIndex = 15 ' 'Label7 ' Me.Label7.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label7.Location = New System.Drawing.Point(0, 168) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(88, 16) Me.Label7.TabIndex = 16 Me.Label7.Text = "Background:" ' 'Label8 ' Me.Label8.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label8.Location = New System.Drawing.Point(0, 200) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(88, 16) Me.Label8.TabIndex = 18 Me.Label8.Text = "Foreground:" ' 'CmbFore ' Me.CmbFore.Items.AddRange(New Object() {"Black", "Red", "Purple", "Blue", "White"}) Me.CmbFore.Location = New System.Drawing.Point(96, 192) Me.CmbFore.Name = "CmbFore" Me.CmbFore.Size = New System.Drawing.Size(96, 21) Me.CmbFore.TabIndex = 17 ' 'BtnDown ' Me.BtnDown.Image = CType(resources.GetObject("BtnDown.Image"), System.Drawing.Image) Me.BtnDown.Location = New System.Drawing.Point(48, 288) Me.BtnDown.Name = "BtnDown" Me.BtnDown.Size = New System.Drawing.Size(20, 21) Me.BtnDown.TabIndex = 23 ' 'BtnUp ' Me.BtnUp.Image = CType(resources.GetObject("BtnUp.Image"), System.Drawing.Image) Me.BtnUp.Location = New System.Drawing.Point(48, 232) Me.BtnUp.Name = "BtnUp" Me.BtnUp.Size = New System.Drawing.Size(20, 21) Me.BtnUp.TabIndex = 22 ' 'BtnLeft ' Me.BtnLeft.Image = CType(resources.GetObject("BtnLeft.Image"), System.Drawing.Image) Me.BtnLeft.Location = New System.Drawing.Point(16, 264) Me.BtnLeft.Name = "BtnLeft" Me.BtnLeft.Size = New System.Drawing.Size(20, 20) Me.BtnLeft.TabIndex = 21 ' 'BtnRight ' Me.BtnRight.Image = CType(resources.GetObject("BtnRight.Image"), System.Drawing.Image) Me.BtnRight.Location = New System.Drawing.Point(80, 264) Me.BtnRight.Name = "BtnRight" Me.BtnRight.Size = New System.Drawing.Size(20, 20) Me.BtnRight.TabIndex = 20 ' 'TxtTimes ' Me.TxtTimes.Location = New System.Drawing.Point(40, 264) Me.TxtTimes.Name = "TxtTimes" Me.TxtTimes.Size = New System.Drawing.Size(34, 20) Me.TxtTimes.TabIndex = 19 Me.TxtTimes.Text = "1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(520, 357) Me.Controls.Add(Me.BtnDown) Me.Controls.Add(Me.BtnUp) Me.Controls.Add(Me.BtnLeft) Me.Controls.Add(Me.BtnRight) Me.Controls.Add(Me.TxtTimes) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.CmbFore) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.CmbBack) Me.Controls.Add(Me.TxtName) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.TxtY) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.TxtX) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Sq) Me.Controls.Add(Me.BtnQuit) Me.Controls.Add(Me.TxtPerim) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.TxtArea) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.TxtSide) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Square World - Class" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' instantiate the square based on button on the form, ' and adjust the textboxes to reflect its current size and position sq1 = New square(Sq) adjustXTextBox() adjustYTextBox() adjustSizeTextBox() End Sub ' adjust text box to reflect where the square really is Private Sub adjustXTextBox() TxtX.Text = sq1.xLocation.ToString() End Sub ' adjust text box to reflect where the square really is Private Sub adjustYTextBox() TxtY.Text = sq1.yLocation.ToString() End Sub ' adjust text box to reflect the square's real size Private Sub adjustSizeTextBox() TxtSide.Text = sq1.sideLen.ToString() End Sub ' handle Go button Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' check if size is ok If IsNumeric(TxtSide.Text) Then Dim len As Integer len = Convert.ToInt32(TxtSide.Text) ' change the square's property sq1.sideLen = len ' calculate and show area and perimeter Dim area As Integer Dim perim As Integer area = sq1.calcArea perim = sq1.calcPerim TxtArea.Text = area.ToString("n0") TxtPerim.Text = perim.ToString("n0") Else MsgBox("I'm sorry the size must be numeric") End If ' check horizontal location If IsNumeric(TxtX.Text) Then Dim x As Integer x = Convert.ToInt32(TxtX.Text) ' change the square's property sq1.xLocation = x End If ' check vertical location If IsNumeric(TxtY.Text) Then Dim y As Integer y = Convert.ToInt32(TxtY.Text) ' change the square's property sq1.yLocation = y End If ' change the square's name property sq1.sqName = TxtName.Text ' adjust the display of the square to match the properties of the square sq1.adjustSquare() End Sub ' exit application Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Close() End Sub ' handles change in background color combobox - change the square's property Private Sub CmbBack_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbBack.SelectedIndexChanged If CmbBack.SelectedItem Is "Gray" Then sq1.backgroundColor = Color.Gray ElseIf CmbBack.SelectedItem Is "Green" Then sq1.backgroundColor = Color.Green ElseIf CmbBack.SelectedItem Is "Blue" Then sq1.backgroundColor = Color.Blue ElseIf CmbBack.SelectedItem Is "Yellow" Then sq1.backgroundColor = Color.Yellow ElseIf CmbBack.SelectedItem Is "Orange" Then sq1.backgroundColor = Color.Orange End If End Sub ' handles change in foreground color combobox - change the square's property Private Sub CmbFore_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbFore.SelectedIndexChanged If CmbFore.SelectedItem Is "Black" Then sq1.foregroundColor = Color.Black ElseIf CmbFore.SelectedItem Is "Red" Then sq1.foregroundColor = Color.Red ElseIf CmbFore.SelectedItem Is "Purple" Then sq1.foregroundColor = Color.Purple ElseIf CmbFore.SelectedItem Is "Blue" Then sq1.foregroundColor = Color.DarkBlue ElseIf CmbFore.SelectedItem Is "White" Then sq1.foregroundColor = Color.White End If End Sub ' check if entered distance is ok - returning whether ok or not - ' side effect is to update distance parameter Private Function checkDistance(ByVal input As String, ByRef distance As Double) As Boolean If IsNumeric(input) Then distance = Convert.ToDouble(input) If distance > 0 Then Return True Else ' can't go nowhere or backwards MsgBox("distance must be positive") Return False End If Else ' non-numeric is not good MsgBox("you must have a number in the text box") Return False End If End Function ' move square up the input amount (assuming it is valid) Private Sub BtnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUp.Click Dim ok As Boolean Dim dist As Double ok = checkDistance(TxtTimes.Text, dist) If ok Then ' move the square and update the location textbox to reflect sq1.moveUp(dist) adjustYTextBox() End If End Sub ' move square right the input amount Private Sub BtnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRight.Click Dim ok As Boolean Dim dist As Double ok = checkDistance(TxtTimes.Text, dist) If ok Then ' move the square and update the location textbox to reflect sq1.moveRight(dist) adjustXTextBox() End If End Sub ' move square down the input amount Private Sub BtnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDown.Click Dim ok As Boolean Dim dist As Double ok = checkDistance(TxtTimes.Text, dist) If ok Then ' move the square and update the location textbox to reflect sq1.moveDown(dist) adjustYTextBox() End If End Sub ' move square left the input amount Private Sub BtnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLeft.Click Dim ok As Boolean Dim dist As Double ok = checkDistance(TxtTimes.Text, dist) If ok Then ' move the square and update the location textbox to reflect sq1.moveLeft(dist) adjustXTextBox() End If End Sub End Class